Got a problem with 'stat' on empty but not 'ejected' drives
llandale - Thu Feb 24 12:02:55 EST 2011 |
Re: "stat" problems
This seems to do the trick:
string res = "NUN"
string drive = "F:"
win32SystemWait_ ("cmd.exe /C dir " drive, res, -1)
res = ansi res
int d1,d2; bool diskInDrive = findPlainText(res, "F:", d1, d2, false)
print "Disc inserted: " diskInDrive "\n"
print "Result: " res "\n"
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
|
Re: "stat" problems Mathias Mamsch - Thu Feb 24 18:38:47 EST 2011
This seems to do the trick:
string res = "NUN"
string drive = "F:"
win32SystemWait_ ("cmd.exe /C dir " drive, res, -1)
res = ansi res
int d1,d2; bool diskInDrive = findPlainText(res, "F:", d1, d2, false)
print "Disc inserted: " diskInDrive "\n"
print "Result: " res "\n"
Mathias Mamsch, IT-QBase GmbH, Consultant for Requirement Engineering and D00RS
Thanks. Turned it into the below (I'm a library function kind-a-guy!). What is the 'ansi' line for?
//***************************************
bool fFiles_IsDriveAvailable(string in_Drive)
{ // Is the Drive avaiable for reading right now?
// in_Drive is a drive letter, looks like "A" or "c" or "D" or "K"
// Origin of this function by Mathias Mamsch posted 2011-Feb-24 here:
// https://www.ibm.com/developerworks/forums/thread.jspa?threadID=362528&tstart=0
// This Function gracefully handles these cases:
// o There is no drive mapped to that letter
// o There is a drive, but no disk inserted at this time
// o You lack 'R'ead rights to the drive
// o Drive recently had a disk in place, but it was removed without properly 'Ejecting' it.
// This is the superiority of this function over attempting 'stat(Drive)' calls,
// which in this case causes the system to prompt you to insert a disk.
if (null in_Drive) return(false)
string Result = "", Drive = ""
bool IsAvail = false
int d1, d2
string COMSPEC = "", SysCommand
Drive = in_Drive ":\\"
COMSPEC = getenv("COMSPEC") // Perhaps will fail in the future
if (null COMSPEC) COMSPEC = "cmd.exe" // This will probably work
SysCommand = COMSPEC " /c dir " Drive
// print "\t\t>>fFiles_IsDriveAvailable, [COMSPEC] [SysCommand] = [" COMSPEC "]\t[" SysCommand "]\n"
win32SystemWait_ (SysCommand, Result, -1)
// Result = ansi(Result)
if (null Result) return(true) //?? win32SystemWait_ didn't work??
IsAvail = findPlainText(Result, Drive, d1, d2, false)
// print "\t>>fFiles_IsDriveAvailable, " Drive "\tDisc inserted: " IsAvail "\tResult: " Result
return(IsAvail)
} // end fFiles_IsDriveAvailable()
//*****************************************
void DoTest(string in_Drive)
{
print in_Drive "\tDisc inserted: " (fFiles_IsDriveAvailable(in_Drive)) "\n"
}
// Expected, Situation, Rationale
DoTest("A") // Yes Drive, no disk
DoTest("C") // OK Yes Drive, yes disk
DoTest("c") // OK lower case OK
DoTest("D") // Yes Drive, disc removed without Ejecting it
DoTest("F") // Yes Drive, no thumb drive inserted
DoTest("K") // OK Yes Mapped network drive
DoTest("M") // No such drive
DoTest("T") // Yes drive, but I have no Read rights
DoTest("4") // Correctly gets error "The system cannot find the path specified."
DoTest("zz") // Correctly gets error "... volume label syntax is incorrect."
DoTest("z5") // Correctly gets error "... volume label syntax is incorrect."
|
Re: "stat" problems Louie, where are you these days? Tara.Wilk@itt.com |